<?php
// +----------------------------------------------------------------------
// | Fanwe 多语商城建站系统 (Build on ThinkPHP)
// +----------------------------------------------------------------------
// | Copyright (c) 2009 http://www.fanwe.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 云淡风轻(97139915@qq.com)
// +----------------------------------------------------------------------

// 地区配置
class GroupCityAction extends CommonAction{
	public function index()
	{
		$map = $this->_search ();
		if (method_exists ( $this, '_filter' )) {
			$this->_filter ( $map );
		}
		$name=$this->getActionName();
		$model = D ($name);
		$this->assign("map",$map);
		
		if (! empty ( $model )) {
			$this->_list( $model, $map);
		}
		
		$this->display ();
	}
	
	//增
	public function add() {
		$new_sort = D(MODULE_NAME)->where("status=1") -> max("sort") + 1;
		$this->assign('new_sort',$new_sort);
		$this->display ();
	}
	
	function insert() {
		//B('FilterString');
		$name=$this->getActionName();
		$model = D ($name);
		$data = $model->create ();
		if ($data === false) {
			$this->error ( $model->getError () );
		}
		
		$py_name = trim($data['py']);
		
		if(empty($py_name))
		{
			$py = new Pinyin();
			$py_name = $py->complie($data['name']);
		}
		
		$data['py_t']=substr($py_name,0,1);
		
		if(M("GroupCity")->where("py='".$py_name."'")->count()>0)
		{
			$py_name = $py_name. "_".(M("GroupCity")->where("py='".$py_name."'")->count()+1);
		}
		$data['py'] = $py_name;
		
		//保存当前数据对象
		if($data['is_default']==1)
		{
			$is_de=M("GroupCity")->where("is_default=1")->count();
			if($is_de){
		 		M("GroupCity")->where("is_default=1")->setField("is_default",0);
			}
		}
		
		$upload_list = $this->uploadFile(1,"city");
		if($upload_list)
		{
			$data['img'] = $upload_list[0]['recpath'].$upload_list[0]['savename'];
		}
		
		$list=$model->add ($data);
		//dump($model->getLastSql());
		if ($list!==false) { //保存成功
			
			$this->saveLog(1,$list);
			$this->success (L('ADD_SUCCESS'));
		} else {
			//失败提示
			$this->saveLog(0,$list);
			$this->error (L('ADD_FAILED'));
		}
	}
	
	function update()
	{
		//B('FilterString');
		$name=$this->getActionName();
		$model = D ($name);
		$data = $model->create ();
		if (false === $data) {
			$this->error ( $model->getError () );
		}
		$py_name = trim($data['py']);
		
		if(empty($py_name))
		{
			$py = new Pinyin();
			$py_name = $py->complie($data['name']);
		}
		
		$data['py_t']=substr($py_name,0,1);
		
		if(M("GroupCity")->where("py='".$py_name."' and id <> ".$data['id'])->count()>0)
		{
			$py_name = $py_name. "_".(M("GroupCity")->where("py='".$py_name."'")->count()+1);
		}
		
		$data['py'] = $py_name;
		
		//保存当前数据对象
		if($data['is_default']==1)
		{
			$is_de=M("GroupCity")->where("is_default=1")->count();
			if($is_de){
		 		M("GroupCity")->where("is_default=1 ")->setField("is_default",0);
			}
		}
		
		$upload_list = $this->uploadFile(1,"city");
		if($upload_list)
		{
			$img = D("GroupCity")->where("id=$data[id]")->getField("img");
			unlink($this->getRealPath().$img);
			$data['img'] = $upload_list[0]['recpath'].$upload_list[0]['savename'];
		}
		
		// 更新数据
		$list=$model->save ($data);
		if (false !== $list) {
			//成功提示
			$this->saveLog(1);
			$this->success (L('EDIT_SUCCESS'));
		} else {
			//错误提示
			$this->saveLog(0);
			$this->error (L('EDIT_FAILED'));
		}
	}
	
	//永久删除
	public function foreverdelete()
	{
		//删除指定记录
		$model = D (MODULE_NAME);
		if (! empty ( $model ))
		{
			$pk = $model->getPk ();
			$id = $_REQUEST [$pk];
			$ids = explode ( ',', $id );
			$names = '';
			$images = array();
			
			foreach($ids as $id)
			{
				$city = M("GroupCity")->where("id=".$id)->field("name,img")->find();
				$names .= $city['name'].",";
				if(!empty($city['img']))
					$images[] = $city['img'];
			}
			
			if($names!='')
			{
				$names = substr($names,0,strlen($names)-1);
			}
			
			if (count($ids) > 0)
			{
				$condition = array ($pk => array ('in',$ids));
				if (false !== $model->where ( $condition )->delete())
				{
					D("CitySite")->where(array('city_id' => array('in',$ids)))->delete();
					foreach($images as $img)
					{
						unlink($this->getRealPath()."/".$img);
					}
					
					$msg = L('DELETE_MSG').$names;
					$this->saveLog(1,0,$msg);
					$this->success ( L('DEL_SUCCESS') );
				}
				else
				{
					$msg = L('DELETE_MSG').$names;
					$this->saveLog(0,0,$msg);
					$this->error (L('DEL_FAILED'));
				}
			}
			else
			{
				$this->saveLog(0);
				$this->error ( L('INVALID_OP') );
			}
		}
		$this->forward ();
	}
	
	public function updateCityRewriteRule()
	{
		$config = require_once $this->getRealPath().'/public/rewrite_config.php';
		$rewrite2File = $this->getRealPath()."/".$config["REWRITE2_FILE"];
		$rewrite3File = $this->getRealPath()."/".$config["REWRITE3_FILE"];
			
		$content2 = file_get_contents($rewrite2File);
		$content3 = file_get_contents($rewrite3File);
		preg_match("/(.*?)#CITYREWRITE_BEGIN.*?#CITYREWRITE_END(.*)/su", $content2, $rewrite2);
		$rewrite2Begin=$rewrite2[1];
		$rewrite2End=$rewrite2[2];
		
		preg_match("/(.*?)#CITYREWRITE_BEGIN.*?#CITYREWRITE_END(.*)/su", $content3, $rewrite3);
		$rewrite3Begin=$rewrite3[1];
		$rewrite3End=$rewrite3[2];
		$ciyt2Rewrite = "";
		$ciyt3Rewrite = "";
		
		$citys = D(MODULE_NAME)->where("status=1")->findAll();
		
		foreach($citys as $city)
		{
			if(!empty($city['py']))
			{
				$ciyt2Rewrite.= preg_replace("/\{city\}/",$city['py'],$config["REWRITE2_RULE"]);
				$ciyt3Rewrite.= preg_replace("/\{city\}/",$city['py'],$config["REWRITE3_RULE"]);
			}
		}
		
		$ciyt2Rewrite = "#CITYREWRITE_BEGIN\r\n".$ciyt2Rewrite;
		$ciyt2Rewrite .= "#CITYREWRITE_END";
		
		$ciyt3Rewrite = "#CITYREWRITE_BEGIN\r\n".$ciyt3Rewrite;
		$ciyt3Rewrite .= "#CITYREWRITE_END";
		
		if(file_put_contents($rewrite2File,$rewrite2Begin.$ciyt2Rewrite.$rewrite2End) && file_put_contents($rewrite3File,$rewrite3Begin.$ciyt3Rewrite.$rewrite3End)){
			//成功提示
			$this->saveLog(1);
			$this->success ("生成规则成功");
		} else {
			//错误提示
			$this->saveLog(0);
			$this->error ("生成规则失败");
		}
		
	}
}
?>